package edu.cuit.campus.web.controller;

import edu.cuit.campus.common.Result;
import jakarta.annotation.Resource;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/19 16:20
 */
@RestController
@RequestMapping("/i18n")
public class I18nRestController {
    @Resource private MessageSource messageSource;

    @GetMapping
    public Result<Map<String, String>> message(@RequestHeader(name = "accept-language", defaultValue = "zh-CN") String acceptLanguage){
        //var resource = new HashMap<String, String>();
        //var locale = LocaleContextHolder.getLocale();
        var locale = Locale.forLanguageTag(acceptLanguage.split(",")[0]);
        var keys = List.of("user.title", "user.name", "user.password", "user.submit");
        var resource = keys.stream().collect(Collectors.toMap(e -> e, e -> messageSource.getMessage(e, null, locale)));
        return Result.ok(resource);
    }
}
